<template>
  <div class="login-container">
    <el-form
      class="login-form"
      :rules="rules"
      ref="form"
      :model="user"
      size="medium"
      @submit.prevent="handleSubmit"
    >
      <!-- <div class="login-form__header">
        <img
          class="login-logo"
          src="@/assets/login_logo.png"
          alt="手机号注册"
        >
      </div> -->
      <el-form-item prop="mobile">
        <el-input
          v-model="user.mobile"
          placeholder="请输入手机号"
        >
          <template #prefix>
            <i class="el-input__icon el-icon-user" />
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="name">
        <el-input
          v-model="user.name"
          placeholder="姓名/昵称"
        >
          <template #prefix>
            <i class="el-input__icon el-icon-user" />
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="pwd">
        <el-input
          v-model="user.pwd"
          type="password"
          placeholder="请输入登录密码"
        >
          <template #prefix>
            <i class="el-input__icon el-icon-lock" />
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="code">
        <div class="imgcode-wrap">
          <el-input
            v-model="user.code"
            placeholder="请输入图形验证码"
          >
            <template #prefix>
              <i class="el-input__icon el-icon-key" />
            </template>
          </el-input>
          <img
            class="imgcode"
            alt="验证码"
            :src="captchaSrc"
            @click="loadcaptcha"
          >
        </div>
      </el-form-item>
      <el-form-item prop="code">
        <div class="imgcode-wrap">
          <el-input
            v-model="user.smsCode"
            placeholder="请输入短信验证码"
          >
            <template #prefix>
              <i class="el-input__icon el-icon-key" />
            </template>
          </el-input>
          <el-button
            type="primary"
            @click="loadsmscode"
          >
            发 送
          </el-button>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button
          class="submit-button"
          type="primary"
          :loading="loading"
          native-type="submit"
        >
          注册
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref, getCurrentInstance } from 'vue'
// import { useStore } from '@/store'
import { getCaptcha, getSmsCode, getSave } from '@/api/common'
// import { useRouter, useRoute } from 'vue-router'
import type { IElForm, IFormItemRule } from '@/types/element-plus'

// const store = useStore()

// const router = useRouter()
// const route = useRoute()
const internalInstance = getCurrentInstance()?.appContext.config.globalProperties

const captchaSrc = ref('')
const smsCode = ref()
const form = ref<IElForm | null>(null)

const user = reactive({
  mobile: '',
  name: '',
  pwd: '',
  code: '',
  smsCode: ''
})

const loading = ref(false)

const rules = ref<IFormItemRule>({
  mobile: [
    { required: true, message: '请输入手机号', trigger: 'change' }
  ],
  name: [
    { required: true, message: '请输入姓名/昵称', trigger: 'change' }
  ],
  pwd: [
    { required: true, message: '请输入登录密码', trigger: 'change' }
  ],
  smsCode: [
    { required: true, message: '请输入短信验证码', trigger: 'change' }
  ]
})

onMounted(() => {
  loadcaptcha()
})

// 获取手机验证码

const loadsmscode = async () => {
  const smsCodeParams = {
    code: user.code,
    mobile: user.mobile,
    k: internalInstance?.config.k
  }
  const data = await getSmsCode(smsCodeParams)
  console.log(data)
  smsCode.value = data
}

// 获取验证码
const loadcaptcha = async () => {
  const data = await getCaptcha()
  captchaSrc.value = URL.createObjectURL(data)
}

const handleSubmit = async () => {
  // 表单验证
  const valid = await form.value?.validate()
  if (!valid) {
    return false
  }

  loading.value = true
  const data = await getSave(user).finally(() => {
    loading.value = false
  })
  console.log(data)

  // store.commit('setUser', {
  //   ...data.user_info,
  //   token: data.token
  // })

  // let redirect = route.query.redirect || '/'
  // if (typeof redirect !== 'string') {
  //   redirect = '/'
  // }

  // router.replace(redirect)

  // 验证通过 展示 loading
  // 请求提交
  // 处理响应
}

</script>

<style lang="scss" scoped>
.login-container {
  min-width: 400px;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #2d3a4b;
}

.login-form {
  padding: 30px;
  border-radius: 6px;
  background: #fff;
  min-width: 350px;
  .login-form__header {
    display: flex;
    justify-content: center;
    align-items: center;
    padding-bottom: 30px;
  }

  .el-form-item:last-child {
    margin-bottom: 0;
  }

  .login__form-title {
    display: flex;
    justify-content: center;
    color: #fff;
  }

  .submit-button {
    width: 100%;
  }

  .login-logo {
    width: 271px;
    height: 74px;
  }
  .imgcode-wrap {
    display: flex;
    align-items: center;
    .imgcode {
      height: 37px;
    }
  }
}
</style>
